//
//  DoctorReserveCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/26.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol DoctorReserveCellDelegate: NSObjectProtocol {
    func doctorReserveCell(_ cell: DoctorReserveCell, willReserveCloud type: DoctorType)
    func doctorReserveCell(_ cell: DoctorReserveCell, willReserveAdvisory type: DoctorType)
}

class DoctorReserveCell: UITableViewCell {
    
    weak var delegate: DoctorReserveCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        bodyView.backgroundColor = selected ? .mainLight : .white
        if selected {
            UIView.animate(withDuration: 0.15) {
                self.setSelected(false, animated: false)
            }
        }
    }
    func configureNurse(model: YSGNurseSubscribeModel){
        avatarView.kf.setImage(with: URL(string: model.doctorPicture ?? ""),
                               placeholder: UIImage(named: "avatar_default"))
        nameLabel.text = model.name
        officeLabel.text = model.jobTitle ?? ""
        departmentLabel.text = model.deptName ?? ""
        scoreLabel.text = model.score ?? ""
        starRatingView.currentStarCount = model.score?.floatValue ?? 0
        recommendView.isHidden = false
        numberLabel.text = "咨询数 " + String(format: "%d", model.consultCount!)
        prescriptionLabel.isHidden = true
        cloudBtn.isHidden = true
        advisoryBtn.isHidden = true
        specialtyLabel.text = model.introduction ?? ""
    }
    func configure(with vm: DoctorReserveRowViewModel, type: DoctorType = .all) {
        avatarView.kf.setImage(with: URL(string: vm.thumbnailUrl),
                               placeholder: UIImage(named: "avatar_default"))
        nameLabel.text = vm.doctorName
        officeLabel.text = vm.office
        specialtyLabel.text = vm.specialty
        scoreLabel.text = vm.score
        departmentLabel.text = vm.department
        prescriptionLabel.text = vm.prescriptionStr
        prescriptionLabel.textColor = vm.prescriptionColor
        starRatingView.currentStarCount = vm.score.floatValue
        recommendView.isHidden = !vm.isRecommend
        numberLabel.text = "咨询数 " + vm.num
        switch type {
        case .all:
            cloudBtn.isHidden = vm.doctorType == .advisory || vm.doctorType == .none
            advisoryBtn.isHidden = vm.doctorType == .cloud || vm.doctorType == .none
        default:
            cloudBtn.isHidden = true
            advisoryBtn.isHidden = true
        }
    }
    
//    @IBOutlet private weak var containerView: UIView! {
//        didSet {
//            containerView.layer.cornerRadius = 16
//            containerView.layer.masksToBounds = true
//        }
//    }
    
    @IBOutlet private weak var avatarView: UIImageView! {
        didSet {
            avatarView.contentMode = .scaleAspectFill
            avatarView.layer.cornerRadius = 13
            avatarView.layer.masksToBounds = true
        }
    }
    @IBOutlet weak var bodyView: UIView! {
        didSet {
            bodyView.layer.shadowColor =  UIColor(red: 0.67, green: 0.72, blue: 0.71, alpha: 0.1).cgColor
            bodyView.layer.shadowOffset = CGSize(width: 0, height: 2)
            bodyView.layer.shadowRadius = 8
            bodyView.layer.shadowOpacity = 1
            bodyView.layer.masksToBounds = false
            bodyView.layer.cornerRadius = 13
        }
    }
    
    @IBOutlet private weak var prescriptionLabel: UILabel!
    
    @IBOutlet private weak var nameLabel: UILabel! {
        didSet {
            nameLabel.textColor = .textDark
            nameLabel.font = .medium_18
        }
    }
    @IBOutlet weak var numberLabel: UILabel!
    
    @IBOutlet private weak var departmentLabel: UILabel! {
        didSet {
            departmentLabel.textColor = .textDark
            departmentLabel.font = .medium_14
        }
    }
    
    @IBOutlet private weak var officeLabel: UILabel! {
        didSet {
            officeLabel.textColor = .textDark
            officeLabel.font = .medium_14
        }
    }
    
    @IBOutlet private weak var specialtyLabel: UILabel! {
        didSet {
            specialtyLabel.textColor = .textLight
            specialtyLabel.font = .regular_14
        }
    }
    
    @IBOutlet private weak var scoreLabel: UILabel!
    
    @IBOutlet private weak var starRatingView: StarRatingView!
    
    @IBOutlet private weak var cloudBtn: UIButton! {
        didSet {
            cloudBtn.setTitleColor(.fontColor, for: .normal)
            cloudBtn.setTitleColor(.white, for: .highlighted)
            cloudBtn.setBackgroundImage(UIImage(color: .white), for: .normal)
            cloudBtn.setBackgroundImage(UIImage(color: .fontColor), for: .highlighted)
            cloudBtn.layer.cornerRadius = cloudBtn.height / 2
            cloudBtn.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var advisoryBtn: UIButton! {
        didSet {
            advisoryBtn.setTitleColor(.fontColor, for: .normal)
            advisoryBtn.setTitleColor(.white, for: .highlighted)
            advisoryBtn.setBackgroundImage(UIImage(color: .white), for: .normal)
            advisoryBtn.setBackgroundImage(UIImage(color: .fontColor), for: .highlighted)
            advisoryBtn.layer.cornerRadius = advisoryBtn.height / 2
            advisoryBtn.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var recommendView: UIImageView!
    
    @IBAction private func cloudAction() {
        delegate?.doctorReserveCell(self, willReserveCloud: .cloud)
    }
    
    @IBAction private func advisoryAction() {
        delegate?.doctorReserveCell(self, willReserveAdvisory: .advisory)
    }
}
